cmake_minimum_required(VERSION 3.2)

set(LIBHTTP_TARGET_NAME H2Lib.http)

ExternalProject_Add(CURL
    PREFIX ${PROJECT_BINARY_DIR}/external
    URL "${PROJECT_SOURCE_DIR}/external/curl-7.49.1.tar.bz2"
    URL_MD5 "6BB1F7AF5B58B30E4E6414B8C1ABCCAB"
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=RELEASE
        -DCMAKE_CXX_FLAGS:CONFIG=${CMAKE_CXX_FLAGS}
        -DCMAKE_CXX_FLAGS_DEBUG:CONFIG=${CMAKE_CXX_FLAGS_DEBUG}
        -DCMAKE_CXX_FLAGS_RELEASE:CONFIG=${CMAKE_CXX_FLAGS_RELEASE}
        -DCMAKE_C_FLAGS:CONFIG=${CMAKE_C_FLAGS}
        -DCMAKE_C_FLAGS_DEBUG:CONFIG=${CMAKE_C_FLAGS_DEBUG}
        -DCMAKE_C_FLAGS_RELEASE:CONFIG=${CMAKE_C_FLAGS_RELEASE}
        -DBUILD_CURL_EXE=OFF
        -DBUILD_CURL_TESTS=OFF
        -DCURL_STATICLIB=ON
        -DHTTP_ONLY=ON
        -DENABLE_IPV6=OFF
        -DENABLE_MANUAL=OFF
        -DCMAKE_USE_OPENSSL=OFF
        -DCMAKE_USE_LIBSSH2=OFF
        -DCMAKE_INSTALL_PREFIX:PATH=${PROJECT_BINARY_DIR}/external
)

add_definitions(-DBUILDING_H2LIB_HTTPCLIENT -DCURL_STATICLIB)
include_directories(
    ${PROJECT_BINARY_DIR}/external/include
    ${PROJECT_SOURCE_DIR}/src
)
link_directories(${PROJECT_BINARY_DIR}/external/lib)

add_library(${LIBHTTP_TARGET_NAME} SHARED
    HttpClient.cpp
    HttpClient.rc
)
add_dependencies(${LIBHTTP_TARGET_NAME} CURL)
target_link_libraries(${LIBHTTP_TARGET_NAME} ws2_32 wldap32 libcurl)

set_target_properties(${LIBHTTP_TARGET_NAME} PROPERTIES
    LINKER_LANGUAGE CXX
    ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib"
    RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin"
)

file(COPY HttpClient.h DESTINATION ${OUTPUT_INCLUDE_DIR}/net)
